package algorithms;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveAction;

public class ParallelEnumSort extends RecursiveAction implements SortStrategy{
    private int[] data;
    private int[] ret;
    private int index;
    private static int c = 64;

    public ParallelEnumSort() {
        ;
    }

    public ParallelEnumSort(int[] data, int[] ret, int index) {
        this.data = data;
        this.index = index;
        this.ret = ret;
    }

    public int[] sort(int[] data) {
        // 排序所调用的方法
        this.data = data.clone();
        this.ret = new int[data.length];

        List<RecursiveAction> tasks = new ArrayList<RecursiveAction>();
        for (int index=0; index<this.data.length; index++) {
            tasks.add(new ParallelEnumSort(this.data, this.ret, index));
        }
        invokeAll(tasks);
        return this.ret;
    }

    public void compute() {
        int place = 0;
        for (int j=0; j<this.data.length; j++) {
            if (this.data[j] < this.data[this.index]) place ++;
            if (this.data[j] == this.data[this.index]) place += (j>index)?1:0;
        }
        this.ret[place] = this.data[index];
        return;
    }

}
